package cn.mazexal.examapi.common.models;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author: Mazexal
 * Date: 2025/7/19
 */
public class SinglePagedResponse<T> extends Response {
    private static final long serialVersionUID = 1L;
    private T data;
    private int total;
    private int pageSize;
    private int current;

    public SinglePagedResponse() {
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getTotal() {
        return this.total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPageSize() {
        return Math.max(this.pageSize, 1);
    }

    public void setPageSize(int pageSize) {
        this.pageSize = Math.max(pageSize, 1);
    }

    public int getCurrent() {
        return this.current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public int getPageCount() {
        return this.total % this.pageSize == 0 ? this.total / this.pageSize : this.total / this.pageSize + 1;
    }

    public static <T> SinglePagedResponse<T> success() {
        SinglePagedResponse<T> response = new SinglePagedResponse();
        response.setSuccess(true);
        return response;
    }

    public static <T> SinglePagedResponse<T> error(String code, String message) {
        SinglePagedResponse<T> response = new SinglePagedResponse();
        response.setSuccess(false);
        response.setErrCode(code);
        response.setErrMessage(message);
        return response;
    }

    public static <T> SinglePagedResponse<T> error(ResponseError error) {
        return error(error.getCode(), error.getMessage());
    }

    public static <T> SinglePagedResponse<T> of(T data, int total, int pageSize, int current) {
        SinglePagedResponse<T> response = new SinglePagedResponse();
        response.setSuccess(true);
        response.setData(data);
        response.setTotal(total);
        response.setPageSize(pageSize);
        response.setCurrent(current);
        return response;
    }

}
